//导入 createSlice 函数
import { createSlice } from "@reduxjs/toolkit";

//创建 slice 对象  
let slice = createSlice({
  //名字
  name: 'zan',
  //初始值
  initialState: {
    value: 100
  },
  //reducer 中的功能
  reducers: {
    //             {type: 'zan/incre', payload: 1}
    incre: (state, action) => {
      //直接修改 state 状态数据, 底层会使用新的对象, 替换旧的状态状态, 使用的工具就是 immer
      state.value += action.payload;
    },
    decre: (state, action) => {
      state.value -= action.payload;
    }
  }
});

//获得 reducer 函数
export default slice.reducer;

//获得 action creator
export let {incre, decre} = slice.actions;
